from flask import Blueprint, Flask
from flaskapp.config import DBconfig
from flaskapp.exts import db
from flaskapp.api.v1 import UserAPI, BookAPI, AdminAPI, MessageAPI, PersonalBookAPI, OnlineReadingAPI, BookReportAPI, \
    PunishmentAPI, on
from flask_cors import CORS

def create_app():
    app = Flask(__name__, static_folder='../static')
    CORS(app)
    register_blueprint(app)
    # 导入数据库配置
    app.config.from_object(DBconfig)
    # 创建数据库 sqlalchemy 工具对象
    db.init_app(app)
    return app

def create_blueprint():
    v1 = Blueprint("v1", __name__)
    UserAPI.api.register(v1)
    BookAPI.api.register(v1)
    AdminAPI.api.register(v1)
    MessageAPI.api.register(v1)
    PersonalBookAPI.api.register(v1)
    OnlineReadingAPI.api.register(v1)
    BookReportAPI.api.register(v1)
    PunishmentAPI.api.register(v1)
    print('create')
    return v1

def register_blueprint(app):
    bp_v1 = create_blueprint()
    app.register_blueprint(bp_v1, url_prefix="/v1")  # url_prefix是给注册的蓝图添加路由前缀。
    print('register')
